package com.elm.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.elm.common.R;
import com.elm.dao.CollectionDao;
import com.elm.dao.StoreDao;
import com.elm.domain.Collection;
import com.elm.domain.Store;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@ResponseBody
@RestController
@RequestMapping("/api/collection")
public class CollectionController {

    @Autowired
    private StoreDao storeDao;

    @Autowired
    private CollectionDao collectionDao;

    @GetMapping
    public R getCollections(@RequestParam(value = "userId",required = true) String userId,@RequestParam(value="current",required = true) Integer current)
    {
        System.out.println("收到了"+userId+"和"+current);
        QueryWrapper<Collection> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("userId",userId);

        IPage<Collection> iPage=new Page<>();
        iPage.setSize(8);
        iPage.setCurrent(current);

        IPage<Collection> page = collectionDao.selectPage(iPage, queryWrapper);

        List<Collection> list=page.getRecords();
        List<Store> stores=new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            QueryWrapper<Store> storeQueryWrapper=new QueryWrapper<>();
            storeQueryWrapper.eq("id",list.get(i).getStoreId())
                    .ne("status",2)
                    .ne("status",3);
            Store store = storeDao.selectById(list.get(i).getStoreId());
            stores.add(store);
        }

        return R.success(page).add("list",stores);
    }

    @PostMapping("/is")
    public R isCollected(@RequestParam(value = "userId",required = true) String userId,@RequestParam(value="storeId",required = true)String storeId)
    {
        QueryWrapper<Collection> queryWrapper=new QueryWrapper<>();

        queryWrapper.eq("userId",userId)
                .eq("storeId",storeId);

        Collection collection = collectionDao.selectOne(queryWrapper);
        if(collection==null)
        {
            return R.success(false);
        }
        return R.success(true);
    }

    @PutMapping
    public R addCollection(@RequestBody Collection collection)
    {
        int insert = collectionDao.insert(collection);

        if(insert<=0) return R.error("出错了");
        return R.success(collection);
    }

    @DeleteMapping
    public R deleteCollection(@RequestBody Collection collection)
    {
        QueryWrapper<Collection> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("userId",collection.getUserId())
                .eq("storeId",collection.getStoreId());

        int delete = collectionDao.delete(queryWrapper);
        if(delete<=0) return R.error("取消收藏失败");
        return R.success(collection);
    }
}
